/*
 *
 *
 */

#include "Animation.h"

#ifndef _ANIMATION_CPP_
#define _ANIMATION_CPP_


/*
 * 
 */
Animation::Animation(void)
{
        frameTime = 0;  /*tempo em milisegundos de um sprite na animação*/
        n = 0;          /*número de sprites no sprite sheet*/
        time = 0;       /*contador de tempo*/
        jumpFrame = 0;  /*indica qual o indice do sprite, no sprite sheet, que sera desenhado*/
        srcRect.x = 0;
        srcRect.y = 0;
        srcRect.w = 0;
        srcRect.h = 0;
        
}

/*
 * 
 */ 
Animation::Animation(const Animation& anim):Sprite(anim), frameTime(anim.frameTime), n(anim.n), time(anim.time), jumpFrame(anim.jumpFrame), srcRect(anim.srcRect)
{
    
}

/*
 * 
 */ 
Animation::Animation(string fileName, double x, double y,int frameTime, int n,double zoom):Sprite(fileName,x,y),frameTime(frameTime),n(n)
{
    time = 0;
    jumpFrame = 0;
    rectangle.w = rectangle.w/n;    /*troca o tamanho do retangulo de destino(janela) para o tamanho de um sprite*/
    /*inicializa o retangulo de origem*/
    srcRect.x = 0;
    srcRect.y = 0;
    srcRect.w = rectangle.w;
    srcRect.h = rectangle.h;
    if(zoom != 1)
        setAnimationZoom(zoom);
        
}

/*
 * 
 */
Animation& Animation::operator=(const Animation &anim)
{
    if (this == &anim) 
        return *this;
    else 
    {
        Sprite::operator=(anim);
        frameTime = anim.frameTime;
        n = anim.n;          
        time = anim.time;       
        jumpFrame = anim.jumpFrame;  
        srcRect = anim.srcRect;
        if(anim.zoom != 1)
            setAnimationZoom(anim.zoom);
        else
            surfaceRotoZoom = NULL;
    }
    return *this;
}


/*
 * 
 */ 
Animation::~Animation(void)
{
    
}

/*
 * 
 */
void Animation::render(SDL_Surface *screen)
{
    if((angle == 0) && (zoom == 1))
    {
        rectangle.x = (int) x /*- rectangle.w/2*/;
        rectangle.y = (int) y /*- rectangle.h/2*/;
        if(SDL_BlitSurface(surface, &srcRect, screen, &rectangle))
            fprintf(stderr,"Erro ao blitar imagem no metodo void Animation::render(SDL_Surface *screen): %s\n%s\n", fileName.c_str(),SDL_GetError());
    }
    else
    {
        rectRotoZoom.x = (int) x /*- (rectRotoZoom.w/2)*/;
        rectRotoZoom.y = (int) y /*- (rectRotoZoom.h/2)*/;
        if(SDL_BlitSurface(surfaceRotoZoom, &srcRect, screen, &rectRotoZoom))
            fprintf(stderr,"Erro ao blitar imagem rotacionada no metodo void Animation::render(SDL_Surface *screen): %s\n%s\n", fileName.c_str(),SDL_GetError());
    }
}

/*
 * 
 */ 
void Animation::update(int tDelta)
{
    time += tDelta;
    jumpFrame += time / frameTime;
    time = time % frameTime;
    if(jumpFrame >= n)
        jumpFrame = jumpFrame % n;
    srcRect.x = jumpFrame * srcRect.w;        
}


/*
 * 
 */ 
SDL_Rect  Animation::getSrcRect(void)
{
    return srcRect;
}

/*
 * 
 */ 
void Animation::setAnimationZoom(double zoom)
{
    setZoom(zoom);                        /*metodo da classe Sprite*/
//cout << rectRotoZoom.w << " " << n << " " << rectRotoZoom.w/n << endl;
    if(n/2 <= rectRotoZoom.w % n )
        rectRotoZoom.w = (rectRotoZoom.w/n) + 1;    /*troca o tamanho do retangulo de destino(janela) para o tamanho de um sprite*/
    else
        rectRotoZoom.w = (rectRotoZoom.w/n);
    /*inicializa o retangulo de origem*/
    srcRect.x = 0;
    srcRect.y = 0;
    srcRect.w = rectRotoZoom.w;
    srcRect.h = rectRotoZoom.h;
}

#endif
